package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/4/9 10:14
 * @apiNote
 */

public class Test00744NextGreatestLetter {
    public char nextGreatestLetter(char[] letters, char target) {
        for (int i = 0; i < letters.length; i++) {
            if (letters[i] > target) {
                return letters[i];
            }
        }
        return letters[0];
    }

    public char nextGreatestLetter1(char[] letters, char target) {
        int length = letters.length;
        if (target >= letters[length - 1]) {
            return letters[0];
        }
        int low = 0, high = length - 1;
        while (low < high) {
            int mid = (high - low) / 2 + low;
            if (letters[mid] > target) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return letters[low];
    }

    public char nextGreatestLetter2(char[] letters, char target) {
        int length = letters.length;
        if (target >= letters[length - 1]) {
            return letters[0];
        }
        int low = 0, high = length - 1;
        while (low <= high) {
            int mid = (high - low) / 2 + low;
            if (letters[mid] > target) {
                high = mid-1;
            } else {
                low = mid + 1;
            }
        }
        return letters[low];
    }

}
